@echo off
:: 设置cmd窗口主题
call lib\theme "file download"

:: 这个脚本主要用作是从远程linux服务器下载指定文件到本地目录，需要配置下相关参数即可
:: 理论上，通过修改参数，可以实现将远程任意文件，下载到本地任意位置
:: 如果本地目录内已经有相同名称的文件，则不会重复下载

:: ============================= 参数列表数-开始 ======================
:: 远程linux服务器IP地址
set HOST=%~1
if "%HOST%" == "" (
	call print\error %LOG_MODULE% "[CHECK] the remote host is null"
 	call print\info %LOG_MODULE% ""
	call print\info %LOG_MODULE% "================= END =================="
	call print\info %LOG_MODULE% ""
 	pause
    exit /b 1
)


:: 登陆用户名
set USER=%~2
if "%USER%" == "" (
	call print\error %LOG_MODULE% "[CHECK] the user name is null"
 	call print\info %LOG_MODULE% ""
	call print\info %LOG_MODULE% "================= END =================="
	call print\info %LOG_MODULE% ""
 	pause
    exit /b 1
)


:: 远程文件所在目录
set REMOTE_DIR=%~3
:: 如果远程目录参数为空，则默认上传到
if "%REMOTE_DIR%" == "" (
	call print\error %LOG_MODULE% "[CHECK] the remot dir is null"
 	call print\info %LOG_MODULE% ""
	call print\info %LOG_MODULE% "================= END =================="
	call print\info %LOG_MODULE% ""
 	pause
    exit /b 1
)
:: 转换斜杠符号，将\替换为/
set REMOTE_DIR=%REMOTE_DIR:\=/%
if "%REMOTE_DIR:~-1%"=="/" set "REMOTE_DIR=%REMOTE_DIR:~0,-1%"
:: 变量为空则重新将斜杠加上
if "%REMOTE_DIR%" == "" (
	set REMOTE_DIR=/
)


:: 远程文件全名
set REMOTE_FILE_NAME=%~4
if "%REMOTE_FILE_NAME%" == "" (
	call print\error %LOG_MODULE% "[CHECK] the remote file name is null"
 	call print\info %LOG_MODULE% ""
	call print\info %LOG_MODULE% "================= END =================="
	call print\info %LOG_MODULE% ""
 	pause
    exit /b 1
)

:: ============================== 参数列表-结束 ======================

:: 本地下载保存目录（建议保持默认不动）
set LOCAL_DIR=%cd%\download\
call lib\tryMkdir %LOCAL_DIR%



:: 获取当前小时
set HOUR=%TIME:~0,2%
:: 小时可能是12小时制，如果小时数字小于10就将数字前的一个空格替换成0
if %HOUR% lss 10 (set HOUR=0%HOUR:~1,2%)
:: 获取当前日期和时间，格式为yyyyMMddHHmmss
set DATETIME_STR=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%%HOUR%%TIME:~3,2%%TIME:~6,2%
:: 随机字符串，包含日期时间和随机数
set RANDOM_STR=%DATETIME_STR%_%RANDOM%
:: 定义临时文件（用于将 plink 命令的输出结果保存到临时文件中）
set TEMP_FIME=temp\temp_download_%RANDOM_STR%.txt
call lib\tryMkdir temp



:: 日志模块名（会作为日志文件名的一部分）
set LOG_MODULE=download

:: 这个变量中保存着4个空格，目的是控制日志样式，请勿删除其中的空格
call print\info %LOG_MODULE% "========= BEGIN FILE DOWNLOAD =========="               
call print\info %LOG_MODULE% ""                                                                                           
call print\info %LOG_MODULE% "    REMOTE_HOST: %HOST%"                                        
call print\info %LOG_MODULE% "    REMOTE_FILE: %REMOTE_DIR%/%REMOTE_FILE_NAME%"  
call print\info %LOG_MODULE% "    LOCAL_DIR: %LOCAL_DIR%"                
call print\info %LOG_MODULE% ""                                              
call print\info %LOG_MODULE% "----------------------------------------"               
call print\info %LOG_MODULE% ""                                              
            

:: 输入远程Linux服务器登录密码
:passwordInput
:: PASSWORD 脚本会自动输入密码
call lib\passwordInput "Please enter the remote host login password" "PASSWORD"
if "%PASSWORD%" == "" (
	goto passwordInput
)



:: 检查本地是否已经存在相同名称的文件，如果存在则提示并退出
if exist "%LOCAL_DIR%\%REMOTE_FILE_NAME%" (
 	call print\info %LOG_MODULE% "[CHECK] file already exists in .\download no need download repeatedly" 
 	call print\info %LOG_MODULE% ""
 	call print\info %LOG_MODULE% "================= END =================="
	call print\info %LOG_MODULE% ""
 	timeout /t 60
    exit /b 0
)

:: 调用plink工具命令开始查询远程文件夹中和文件是否存在, 将查询结果保存到临时文件中 0 表示文件存在 空表示文件不存在
lib\plink -batch -pw %PASSWORD% %USER%@%HOST% "test -d %REMOTE_DIR% && test -e %REMOTE_DIR%/%REMOTE_FILE_NAME% && echo 0" > %TEMP_FIME%
if "%errorlevel%" neq "0" (
	:: 删除临时文件
	del %TEMP_FIME%
	call print\error %LOG_MODULE% "[CHECK] test file exist failed"
 	call print\info %LOG_MODULE% ""
	call print\info %LOG_MODULE% "================= END =================="
	call print\info %LOG_MODULE% ""
 	pause
    exit /b 1
)


:: 读取临时文件中第一行数据保存到 TEST_FILE_RESULT 中 0 表示文件存在 空表示文件不存在
for /F "delims=" %%H in (%TEMP_FIME%) do set TEST_FILE_RESULT=%%H
:: 删除临时文件
del %TEMP_FIME%
:: 如果远程文件不存在 则提示并退出
if "%TEST_FILE_RESULT%" neq "0" (
	call print\error %LOG_MODULE% "[CHECK] TEST_FILE_RESULT: %TEST_FILE_RESULT%"
	call print\error %LOG_MODULE% "[CHECK] the remote file does not exist" 
 	call print\info %LOG_MODULE% ""
 	call print\info %LOG_MODULE% "================= END =================="
	call print\info %LOG_MODULE% ""
 	pause
    exit /b 1
)

call print\info %LOG_MODULE% "[DOWNLOAD] to be download file from remote..." 
:: 调用 pscp 工具命令开始下载
lib\pscp -pw %PASSWORD% %USER%@%HOST%:%REMOTE_DIR%/%REMOTE_FILE_NAME% %LOCAL_DIR%\%REMOTE_FILE_NAME%
:: errorlevel 是bat批处理内置变量，表示上一条命令执行是否成功 0：成功 其他非0值表示失败
if "%errorlevel%" neq "0" (
	call print\error %LOG_MODULE% "[DOWNLOAD] download failed." 
	call print\info %LOG_MODULE% ""
	call print\info %LOG_MODULE% "================= END ==================" 
	call print\info %LOG_MODULE% ""
 	pause
 	exit	 
)

:: 二次校验备份的文件是否已下载到本地，不存在则提示并退出
if not exist "%LOCAL_DIR%\%REMOTE_FILE_NAME%" (
	call print\error %LOG_MODULE% "[DOWNLOAD] download failed."
	call print\info %LOG_MODULE% ""
	call print\info %LOG_MODULE% "================= END =================="
	call print\info %LOG_MODULE% ""
	pause
	exit
)

:: 下载成功后提示文件位置
call print\info %LOG_MODULE% "[DOWNLOAD] download file in dir: %LOCAL_DIR%%REMOTE_FILE_NAME%"
call print\info %LOG_MODULE% "[DOWNLOAD] download successful."
call print\info %LOG_MODULE% ""
call print\info %LOG_MODULE% "================= END =================="
call print\info %LOG_MODULE% ""
timeout /t 60
exit